﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dos.Tool.Common
{
    /// <summary>
    /// makedown文件格式化工具
    /// </summary>
    public class MDFormatHelp
    {
        /// <summary>
        /// MD 文档
        /// </summary>
        private List<MDRow> m_MD;
        private string m_Str;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="str">需要格式化的文字</param>
        public MDFormatHelp(string str)
        {
            m_Str = str;
            Analay(str);
        }

        private void Analay(string str)
        {
            try
            {

                if (!string.IsNullOrEmpty(str))
                {
                    m_MD = new List<MDRow>();
                    string[] rows = str.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    //遍历每行
                    foreach (var row in rows)
                    {
                        string myRow= row;
                        //首先移除每行开始的"|"
                        if (myRow.StartsWith("|"))
                        {
                            myRow = myRow.Substring(1);
                        }
                        //移除每行结束的|
                        if (myRow.EndsWith("|"))
                        {
                            myRow = myRow.Substring(0,myRow.Length-1);
                        }

                        MDRow mdRow = new MDRow();
                        //返回每行内容数组
                        string[] contents = myRow.Split(new char[] { '|' }, StringSplitOptions.None);
                        for (int i = 0; i < contents.Length; i++)
                        {
                            string content = contents[i].Trim();//每项的内容
                            int charLength = 0;
                            int zhLength = 0;
                            int sum = 0;
                            sum = GetStringLength(content,out charLength,out zhLength);

                            MDContent mDContent = new MDContent
                            {
                                Content = content,
                                Length = sum,
                                CharLength = charLength,
                                ZhLength = zhLength
                            };
                            mdRow.Contents.Add(mDContent);
                        }
                        m_MD.Add(mdRow);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 或者字符串长度
        /// </summary>
        /// <param name="str">输入字符串</param>
        /// <param name="charlength">英文字符数量</param>
        /// <param name="zhlength">中文字符数量</param>
        /// <returns>字节总长度</returns>
        private int GetStringLength(string str,out int charlength,out int zhlength)
        {
            charlength = 0;
            zhlength = 0;
            if (str.Length == 0)
                return 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] s = ascii.GetBytes(str);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    zhlength += 1;
                }
                else
                {
                    charlength += 1;
                }
            }
            return zhlength * 2 + charlength;
        }

        public string Format()
        {
            try
            {
                //首先求出总列数
                int colSumNo = (from s in m_MD
                                select s.CloumsNo).Max();
                List<int> colNo = new List<int>();
                //首先计算出每列最大的数量

                for (int i = 0; i < colSumNo; i++)
                {
                    int max = 0;

                    for (int j = 0; j < m_MD.Count; j++)
                    {
                        if (i < m_MD[j].CloumsNo)
                        {
                            int CloumsNo = m_MD[j].Contents[i].Length;
                            if (max < CloumsNo)
                            {
                                max = CloumsNo;
                            }
                        }
                    }
                    colNo.Add(max);
                }

                //拼成字符串
                StringBuilder sb = new StringBuilder();


                for (int i = 0; i < m_MD.Count; i++)
                {
                    if (i==1)
                    {
                        sb.AppendLine(m_MD[i].ToSpliteString(colNo));

                    }
                    else
                    {
                        sb.AppendLine(m_MD[i].ToString(colNo));

                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
    }


    /// <summary>
    /// MD行
    /// </summary>
    public class MDRow
    {
        public List<MDContent> Contents { get; set; }

        public MDRow()
        {
            Contents = new List<MDContent>();
        }

        /// <summary>
        /// 返回列数
        /// </summary>
        public int CloumsNo
        {
            get { return Contents.Count; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colMax"></param>
        /// <returns></returns>
        public  string ToString(List<int> colMax )
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < CloumsNo; i++)
            {
                sb.Append("|");
                sb.Append(Contents[i].ToMDstr(colMax[i]));
;            }
            sb.Append("|");
            return sb.ToString();
        }

        public string ToSpliteString(List<int> colMax)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < CloumsNo; i++)
            {
                sb.Append("|");
                sb.Append(Contents[i].ToSplite(colMax[i]));
            }
            sb.Append("|");
            return sb.ToString();
        }

    }

    public class MDContent
    {
        public string Content { get; set; }
        public int Length { get; set; }
        /// <summary>
        /// 英文字符数量
        /// </summary>
        public int CharLength { get; set; }
        /// <summary>
        /// 中文字符数量
        /// </summary>
        public int ZhLength { get; set; }

        public string ToMDstr(int colMax)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" ");//开头加空格
            sb.Append(Content.PadRight(colMax - ZhLength+1));//增加内容
            //sb.Append("".PadLeft(colMax-ZhLength));//增加空格对齐
            sb.Append(""); //结尾加
            return sb.ToString();
        }

        /// <summary>
        /// 返回分隔符行
        /// </summary>
        /// <param name="colMax"></param>
        /// <returns></returns>
        public string ToSplite(int colMax)
        {
            string c = "";
            c =  c.PadRight(colMax, '-'); //使用 - 填充
            //两端加空格
            c = c.Insert(0, " ");
            c = c.Insert(c.Length, " ");
            return c;
        }
    }
}